﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
using System.IO;

namespace ParamSetCollection.General.Tools
{
    public class ParamsFileCreateHelper
    {

        public static void CreateParamsFile(Dictionary<string, Parameters.Normal.ParameterUnit> paramDic, Type type, string path, bool remoteEn = false)
        {
            FileInfo fi = new FileInfo(path);

            if (Directory.Exists(fi.Directory.FullName) == false)
            {
                Directory.CreateDirectory(fi.Directory.FullName);
            }


            try
            {
                //if (File.Exists(path))
                //{
                //    MessageBoxResult rs = MessageBox.Show("over write ?", "", MessageBoxButton.YesNo);
                //    if (rs != MessageBoxResult.Yes)
                //    {
                //        return;
                //    }
                //}


                XmlDocument doc = new XmlDocument();

                XmlElement xmlNodConfig = doc.CreateElement("ParamsList");

                XmlElement paramNod = null;

                foreach (var key in paramDic.Keys)
                {
                    paramNod = doc.CreateElement(key);//code

                    XmlElement unitNod = doc.CreateElement("Name_cn");
                    unitNod.InnerText = key;
                    paramNod.AppendChild(unitNod);
                    unitNod = null;

                    unitNod = doc.CreateElement("Name_en");
                    unitNod.InnerText = key;
                    paramNod.AppendChild(unitNod);
                    unitNod = null;

                    unitNod = doc.CreateElement("MesName");
                    unitNod.InnerText = key;
                    paramNod.AppendChild(unitNod);
                    unitNod = null;

                    unitNod = doc.CreateElement("RemoteEnable");
                    unitNod.InnerText = key.Contains("Empty") ? false.ToString() : remoteEn.ToString();
                    paramNod.AppendChild(unitNod);
                    unitNod = null;

                    unitNod = doc.CreateElement("PlcRenewLogEnable");
                    unitNod.InnerText = "False";
                    paramNod.AppendChild(unitNod);
                    unitNod = null;

                    unitNod = doc.CreateElement("Times");
                    unitNod.InnerText = "1";
                    paramNod.AppendChild(unitNod);
                    unitNod = null;

                    unitNod = doc.CreateElement("Unit");
                    unitNod.InnerText = "N/A";
                    paramNod.AppendChild(unitNod);
                    unitNod = null;

                    unitNod = doc.CreateElement("Type");
                    unitNod.InnerText = "N/A";
                    paramNod.AppendChild(unitNod);
                    unitNod = null;

                    if (type == typeof(bool))
                    {
                        unitNod = doc.CreateElement("Format");
                        unitNod.InnerText = "%b";
                        paramNod.AppendChild(unitNod);
                        unitNod = null;

                        unitNod = doc.CreateElement("Value");
                        unitNod.InnerText = "false";
                        paramNod.AppendChild(unitNod);
                        unitNod = null;

                        unitNod = doc.CreateElement("Max");
                        unitNod.InnerText = "true";
                        paramNod.AppendChild(unitNod);
                        unitNod = null;

                        unitNod = doc.CreateElement("Min");
                        unitNod.InnerText = "false";
                        paramNod.AppendChild(unitNod);
                        unitNod = null;
                    }
                    else
                    {
                        unitNod = doc.CreateElement("Format");
                        unitNod.InnerText = "%d";
                        paramNod.AppendChild(unitNod);
                        unitNod = null;

                        unitNod = doc.CreateElement("Value");
                        unitNod.InnerText = "0";
                        paramNod.AppendChild(unitNod);
                        unitNod = null;

                        unitNod = doc.CreateElement("Max");
                        unitNod.InnerText = "10000";
                        paramNod.AppendChild(unitNod);
                        unitNod = null;

                        unitNod = doc.CreateElement("Min");
                        unitNod.InnerText = "0";
                        paramNod.AppendChild(unitNod);
                        unitNod = null;
                    }

                    xmlNodConfig.AppendChild(paramNod);
                }

                doc.AppendChild(xmlNodConfig);

                FileInfo file = new FileInfo(path);
                XmlTextWriter tr = null;
                if (!file.Exists)
                {
                    tr = new XmlTextWriter(path, System.Text.Encoding.UTF8);
                    tr.Formatting = Formatting.Indented;
                    doc.WriteContentTo(tr);
                    tr.Close();
                }
                else
                {

                    FileStream stream = file.Open(FileMode.Truncate);
                    tr = new XmlTextWriter(stream, null);
                    tr.Formatting = Formatting.Indented;
                    doc.WriteContentTo(tr);
                    tr.Close();
                    stream.Close();
                }
            }
            catch (Exception ex)
            {

                throw;
            }


        }

        //public static void AppendParamsFile(Dictionary<string, string> paramDic, Type type, string path, bool remoteEn = false)
        //{
        //    FileInfo fi = new FileInfo(path);

        //    if (Directory.Exists(fi.Directory.FullName) == false)
        //    {
        //        Directory.CreateDirectory(fi.Directory.FullName);
        //    }

        //    try
        //    {
        //        if (File.Exists(path))
        //        {
        //            XmlDocument doc = new XmlDocument();

        //            doc.Load(path);

        //            XmlNode nod = doc.SelectSingleNode("ParamsList");
        //            //XmlNodeList nodList = nod.ChildNodes;//获取路径参数列表
        //            if (nod == null)
        //            {
        //                MessageBox.Show(path + " is error");
        //                return;
        //            }

        //            XmlElement paramNod = null;

        //            bool UpdateMark = false;
        //            foreach (var key in paramDic.Keys)
        //            {
        //                XmlNode nod2 = doc.SelectSingleNode("ParamsList/" + key);

        //                if (nod2 != null)
        //                {
        //                    continue;
        //                }

        //                UpdateMark = true;

        //                paramNod = doc.CreateElement(key);//code

        //                XmlElement unitNod = doc.CreateElement("Name_cn");
        //                unitNod.InnerText = key;
        //                paramNod.AppendChild(unitNod);
        //                unitNod = null;

        //                unitNod = doc.CreateElement("Name_en");
        //                unitNod.InnerText = key;
        //                paramNod.AppendChild(unitNod);
        //                unitNod = null;

        //                unitNod = doc.CreateElement("MesName");
        //                unitNod.InnerText = key;
        //                paramNod.AppendChild(unitNod);
        //                unitNod = null;

        //                unitNod = doc.CreateElement("RemoteEn");
        //                unitNod.InnerText = remoteEn.ToString();
        //                paramNod.AppendChild(unitNod);
        //                unitNod = null;

        //                unitNod = doc.CreateElement("Unit");
        //                unitNod.InnerText = "N/A";
        //                paramNod.AppendChild(unitNod);
        //                unitNod = null;

        //                unitNod = doc.CreateElement("Type");
        //                unitNod.InnerText = "N/A";
        //                paramNod.AppendChild(unitNod);
        //                unitNod = null;

        //                if (type == typeof(bool))
        //                {
        //                    unitNod = doc.CreateElement("Format");
        //                    unitNod.InnerText = "%b";
        //                    paramNod.AppendChild(unitNod);
        //                    unitNod = null;

        //                    unitNod = doc.CreateElement("Value");
        //                    unitNod.InnerText = "false";
        //                    paramNod.AppendChild(unitNod);
        //                    unitNod = null;

        //                    unitNod = doc.CreateElement("Max");
        //                    unitNod.InnerText = "true";
        //                    paramNod.AppendChild(unitNod);
        //                    unitNod = null;

        //                    unitNod = doc.CreateElement("Min");
        //                    unitNod.InnerText = "false";
        //                    paramNod.AppendChild(unitNod);
        //                    unitNod = null;
        //                }
        //                else
        //                {
        //                    unitNod = doc.CreateElement("Format");
        //                    unitNod.InnerText = "%d";
        //                    paramNod.AppendChild(unitNod);
        //                    unitNod = null;

        //                    unitNod = doc.CreateElement("Value");
        //                    unitNod.InnerText = "0";
        //                    paramNod.AppendChild(unitNod);
        //                    unitNod = null;

        //                    unitNod = doc.CreateElement("Max");
        //                    unitNod.InnerText = "10000";
        //                    paramNod.AppendChild(unitNod);
        //                    unitNod = null;

        //                    unitNod = doc.CreateElement("Min");
        //                    unitNod.InnerText = "0";
        //                    paramNod.AppendChild(unitNod);
        //                    unitNod = null;
        //                }

        //                nod.AppendChild(paramNod);
        //            }

        //            //doc.AppendChild(xmlNodConfig);

        //            if (UpdateMark == false)
        //            {
        //                return;
        //            }

        //            FileInfo file = new FileInfo(path);
        //            XmlTextWriter tr = null;
        //            if (!file.Exists)
        //            {
        //                tr = new XmlTextWriter(path, System.Text.Encoding.UTF8);
        //                tr.Formatting = Formatting.Indented;
        //                doc.WriteContentTo(tr);
        //                tr.Close();
        //            }
        //            else
        //            {

        //                FileStream stream = file.Open(FileMode.Truncate);
        //                tr = new XmlTextWriter(stream, null);
        //                tr.Formatting = Formatting.Indented;
        //                doc.WriteContentTo(tr);
        //                tr.Close();
        //                stream.Close();
        //            }
        //        }
        //    }
        //    catch (Exception ex)
        //    {

        //        throw;
        //    }
        //}

    }
}
